package com.bolt.admin.properties;

import com.bolt.admin.exception.BadConfigurationException;
import com.bolt.common.utils.StrUtil;
import com.wf.captcha.*;
import com.wf.captcha.base.Captcha;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.awt.*;
import java.util.Objects;

/**
 * Created by Administrator on 2020/9/15.
 */
@Data
@ConfigurationProperties(prefix = "login", ignoreUnknownFields = true)
public class LoginProperties {

    private CaptchaConfig captchaCode;

    private JwtConfig jwt;

    private CookieConfig cookie;

    private SessionConfig session;

    /**
     * 尝试登录失败限制次数（默认3次数）
     */
    private int logonFailureLimit=3;

    /**
     * 用户锁定时间（默认5分钟）
     */
    private long lockedTime=5;

    /**
     * 获取验证码生产类
     *
     * @return /
     */
    public Captcha getCaptcha() {
        if (Objects.isNull(captchaCode)) {
            captchaCode = new CaptchaConfig();
            if (Objects.isNull(captchaCode.getCodeType())) {
                captchaCode.setCodeType(CaptchaConfig.CaptchaEnum.arithmetic);
            }
        }
        return switchCaptcha(captchaCode);
    }

    /**
     * 依据配置信息生产验证码
     *
     * @param captchaCode 验证码配置信息
     * @return /
     */
    private Captcha switchCaptcha(CaptchaConfig captchaCode) {
        Captcha captcha;
        synchronized (this) {
            switch (captchaCode.getCodeType()) {
                case arithmetic:
                    // 算术类型 https://gitee.com/whvse/EasyCaptcha
                    captcha = new ArithmeticCaptcha(captchaCode.getWidth(), captchaCode.getHeight());
                    // 几位数运算，默认是两位
                    captcha.setLen(captchaCode.getLength());
                    break;
                case chinese:
                    captcha = new ChineseCaptcha(captchaCode.getWidth(), captchaCode.getHeight());
                    captcha.setLen(captchaCode.getLength());
                    break;
                case chinese_gif:
                    captcha = new ChineseGifCaptcha(captchaCode.getWidth(), captchaCode.getHeight());
                    captcha.setLen(captchaCode.getLength());
                    break;
                case gif:
                    captcha = new GifCaptcha(captchaCode.getWidth(), captchaCode.getHeight());
                    captcha.setLen(captchaCode.getLength());
                    break;
                case spec:
                    captcha = new SpecCaptcha(captchaCode.getWidth(), captchaCode.getHeight());
                    captcha.setLen(captchaCode.getLength());
                    break;
                default:
                    throw new BadConfigurationException("验证码配置信息错误！正确配置查看 switchCaptcha ");
            }
        }
        if (StrUtil.isNotBlank(captchaCode.getFontName())) {
            captcha.setFont(new Font(captchaCode.getFontName(), Font.PLAIN, captchaCode.getFontSize()));
        }
        captcha.setWidth(captchaCode.getWidth());
        captcha.setHeight(captcha.getHeight());
        return captcha;
    }
}

 